/* Copyright 2007 Gearbox Software - http://www.gearboxsoft.com
 
 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at
 
     http:www.apache.org/licenses/LICENSE-2.0
 
 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.*/
/*
 * Created by: 
 * Created: Wednesday, May 09, 2007
 */

namespace GearBlog.Web.Providers
{
	using System.Net;
	using System.Net.Mail;
	using Castle.Components.Common.EmailSender;
	using Castle.MonoRail.Framework.Configuration;
	using GearBlog.Web.Models;
	using GearBlog.Web.Utils;

	public class EmailNotificationProvider:INotificationProvider
	{
		private Notifier _notifier;
		public void SetNotifier(Notifier notifier)
		{
			_notifier = notifier;
		}

		public void Notify(Post post, User[] users)
		{
			_notifier.CurrentController.PropertyBag["Post"] = post;
			_notifier.CurrentController.PropertyBag["Title"] = _notifier.CurrentController.Setting.Title;
			_notifier.CurrentController.PropertyBag["ownerEmail"] = _notifier.CurrentController.Setting.OwnerEmail;
			Message message = _notifier.CurrentController.RenderMailMessage("postnotify");
			// we will use our own smtp sender as MR does not support multiple recipients
			// retrieve monorail config so we can use smtp server
			MonoRailConfiguration config = MonoRailConfiguration.GetConfig();

			SmtpClient client = new SmtpClient(config.SmtpConfig.Host, config.SmtpConfig.Port);
			if (!string.IsNullOrEmpty(config.SmtpConfig.Username))
				client.Credentials = new NetworkCredential(config.SmtpConfig.Username, config.SmtpConfig.Password);

			MailMessage mail=new MailMessage();
			mail.From = new MailAddress(message.From);
			mail.Subject = message.Subject;
			mail.Body = message.Body;

			FillAddressList(mail,users);

			client.Send(mail);
		}
		private void FillAddressList(MailMessage mail, User[] users)
		{
			foreach (User user in users)
			{
				mail.Bcc.Add(new MailAddress(user.Email,user.FullName));		
			}
		}


		public void Notify(Comment comment, User[] users)
		{
			_notifier.CurrentController.PropertyBag["Comment"] = comment;
			_notifier.CurrentController.PropertyBag["Title"] = _notifier.CurrentController.Setting.Title;
			_notifier.CurrentController.PropertyBag["ownerEmail"] = _notifier.CurrentController.Setting.OwnerEmail;
			Message message = _notifier.CurrentController.RenderMailMessage("commentnotify");
			// we will use our own smtp sender as MR does not support multiple recipients
			// retrieve monorail config so we can use smtp server
			MonoRailConfiguration config = MonoRailConfiguration.GetConfig();

			SmtpClient client = new SmtpClient(config.SmtpConfig.Host, config.SmtpConfig.Port);
			if (!string.IsNullOrEmpty(config.SmtpConfig.Username))
				client.Credentials = new NetworkCredential(config.SmtpConfig.Username, config.SmtpConfig.Password);

			MailMessage mail = new MailMessage();
			mail.From = new MailAddress(message.From);
			mail.Subject = message.Subject;
			mail.Body = message.Body;

			FillAddressList(mail, users);

			client.Send(mail);
		}
		
	}
}